package org.sction;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import javax.sql.DataSource;

/**
 * @author 作者 : Administrator
 * @version 创建时间：2013年8月19日 下午5:16:43
 * 
 *          类说明
 */
public class ConfigTools {
	private DataSource dataSource;

	public static void initProperties() {
		// 目录信息
		String projectpath = new File("").getAbsolutePath();
		System.setProperty("projectpath", projectpath);
		System.out.println("项目目录：" + System.getProperty("projectpath"));
		String classpath = new File(ClassLoader.getSystemClassLoader()
				.getResource("").getFile()).getPath();
		System.setProperty("classpath", classpath);
		System.out.println("类目录：" + System.getProperty("classpath"));
		System.setProperty("rootpath",
				projectpath.substring(0, projectpath.indexOf(File.separator)));
		System.out.println("所在根目录：" + System.getProperty("rootpath"));
		System.out.println("用户目录：" + System.getProperty("user.dir"));
		System.out.println("JDK目录：" + System.getProperty("java.home"));
		System.out.println("临时目录：" + System.getProperty("java.io.tmpdir"));
		System.out.println("endorsed目录："
				+ System.getProperty("java.endorsed.dirs"));

		// global.properties信息
		String[] pathx = new String[] { System.getProperty("rootpath"),
				System.getProperty("projectpath"),
				System.getProperty("classpath"), System.getProperty("user.dir") };
		for (String path : pathx) {
			Properties config = new Properties();// 记录配置项
			try {
				FileInputStream fin = new FileInputStream(path);
				config.load(fin);
				fin.close();
				System.setProperties(config);
			} catch (Exception e) {
			}
		}
	}

	public static void main(String[] args) {
		System.getProperties();
		initProperties();
	}

	public static String getRootPath(String[] args) {
		return args.length > 0 ? (args[0] + "/") : ConfigTools.class
				.getClassLoader().getResource("").getFile();
	}

	public static String getPropertiesPath(String[] args) {
		String path = null;
		try {
			path = getRootPath(args) + "global.properties";
			File file = new File(path);
			if (file.exists()) {
				return file.getPath();
			}
		} catch (Exception e) {
		}
		try {
			path = getRootPath(args) + "global_zh_CN.properties";
			File file = new File(path);
			if (file.exists()) {
				return file.getPath();
			}
		} catch (Exception e) {
		}
		return null;
	}

	public static Properties getProperties(String path) {
		Properties config = new Properties();// 记录配置项
		try {
			FileInputStream fin = new FileInputStream(path);
			config.load(fin); // 载入文件
			fin.close();
		} catch (Exception e) {
			return null;
		}
		return config;
	}

	public static String getPropertie(Properties config, String key) {
		if (config != null) {
			Object obj = config.getProperty(key);
			if (obj != null) {
				return obj.toString();
			}
		}
		return null;
	}

	// public static void main(String[] args) throws IOException {
	// String path = getPropertiesPath(args);
	// if (path != null) {
	// Properties config = getProperties(path);// 记录配置项
	// if (config != null) {
	// Console cons = System.console();
	// while (true) {
	// System.out.print("输入配置名：");
	// String key = cons.readLine();
	// if (key.equals("bye")) {
	// System.out.println("byebye!");
	// System.exit(0);
	// }
	// System.out.print("输入配置值：");
	// String value = cons.readLine();
	// if (key.equals("bye")) {
	// System.out.println("byebye!");
	// System.exit(0);
	// }
	// Object obj = config.get(key);
	// if (obj == null) {
	// config.put(key, value);
	// } else {
	// config.setProperty(key, value);
	// }
	// FileOutputStream out = new FileOutputStream(path);
	// config.store(out, "ConfigTools");
	// out.close();
	// }
	// }
	// }
	// }
}
